<template>
    <div class="login_container">
        <div class="login_box">
            <!--头像区域-->
            <div class="avatar_box">
                <img src="../assets/logo.png" alt="">
            </div>
            <!--通过:rules绑定校验规则对象-->
            <el-form ref="formRef" :rules="rules" :model="form" class="form_box" status-icon>
                <!--通过prop绑定校验规则-->
                <el-form-item prop="username">
                    <el-input prefix-icon="iconfont icon-yonghu" v-model="form.username"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input prefix-icon="iconfont icon-mima" type="password" v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item class="btn_item">
                    <el-button type="primary" @click="login">登入</el-button>
                    <el-button type="info" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
        data() {
            return {
                form: {
                    username: '',
                    password: ''
                },
                rules: {
                    username: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                        {min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur'}
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'change'},
                        {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
                    ]
                }
            };
        },
        methods: {
            login() {
                this.$refs.formRef.validate(async validate => {
                    console.log('校验是否成功: ' + validate);
                    if (validate) {
                        // 采用await和async异步登入
                        // 假装登入成功了
                        this.$message.success('登入成功');
                        window.sessionStorage.setItem('token', 'admin$123456#login');
                        // 跳转到我们的home页面
                        this.$router.push('/home');
                        /*const {data: res} = await this.$http.post('login', this.form);
                        // {data: res}: 将data属性解构出来赋值给res对象
                        console.log(res);
                        if (parseInt(res.code) !== 200) {
                            return this.$message.error(res.msg);
                        }
                        window.sessionStorage.setItem('token', res.data);
                        return this.$message.success('登入成功');*/
                    } else {
                        return this.$message.warning('请填写正确的数据');
                    }
                });
            },
            reset() {
                // 通过引用组件重置表单 :ref = formRef
                this.$refs.formRef.resetFields();
            }
        }
    }
</script>

<style lang="less" scoped>
    .login_container {
        background-color: #2d4d6d;
        height: 100%;
    }

    .login_box {
        width: 450px;
        height: 300px;
        background-color: #fff;
        border-radius: 3px;
        position: absolute;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
    }

    .avatar_box {
        height: 130px;
        width: 130px;
        border: 1px solid #eee;
        -webkit-border-radius: 50%;
        -moz-border-radius: 50%;
        border-radius: 50%;
        padding: 10px;
        box-shadow: 0 0 10px #DDD;
        position: absolute;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #FFF;

        img {
            height: 100%;
            width: 100%;
            -webkit-border-radius: 50%;
            -moz-border-radius: 50%;
            border-radius: 50%;
            background-color: #EEE;
        }
    }

    .btn_item {
        display: flex;
        justify-content: flex-end;
    }

    .form_box {
        position: absolute;
        bottom: 0;
        width: 100%;
        padding: 0 20px;
        box-sizing: border-box;
    }

</style>
